﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;
using MainPath;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace MainFile
{
    public static class MyFile
    {
        public static WebClient mywebclient = new WebClient();
        
        public static Image GetImageForUrl(string url)
        {
            string FilePath = MyPath.GetNewImage(Path.GetExtension(url));
            mywebclient.DownloadFile(url,FilePath);
            return Image.FromFile(FilePath);
        }

        public static string GetFileToString(string filepath) 
        {
            string tem = string.Empty;
            using(StreamReader sr=new StreamReader(filepath))
            {
                tem = sr.ReadToEnd();
            }
            return tem;
        }

        public static string DownFileZipSolution(string file)
        {
            string FilePath = MyPath.GetSolutionZip(file);
            string dir = MyPath.GetZipForDirPath(FilePath);
            if (Directory.Exists(dir)) 
            {
                Directory.Delete(dir, true);
            }
            return DownFileZip(file, FilePath);
        }
        public static bool DownFile(string soruce,string target)
        {
            try
            {
                mywebclient.DownloadFile(soruce, target);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }
        private static string DownFileZip(string file, string FilePath)
        {
            if (File.Exists(FilePath)) 
            {
                File.Delete(FilePath);
            }
            mywebclient.DownloadFile(file, FilePath);
            string dir = Path.GetDirectoryName(FilePath)+"\\";
            string unpackdir = UnpackFiles(FilePath,dir);
            return unpackdir + "\\";
        }
        public static string DownFileZipComponent(string solutionname,string file) 
        {
            string FilePath = MyPath.GetComponentZip(solutionname,file);
            return DownFileZip(file, FilePath); 
        }
        /// <summary>
        /// 解压缩文件
        /// </summary>
        /// <param name="zipFile">待解压缩的文件</param>
        /// <param name="directory">解压缩后文件存放的目录</param>
        public static string UnpackFiles(string zipFile, string directory)
        {
            string unpackdir = string.Empty;
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            ZipInputStream zis = new ZipInputStream(File.OpenRead(zipFile));
            ZipEntry theEntry = null;
            while ((theEntry = zis.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(theEntry.Name);
                string fileName = Path.GetFileName(theEntry.Name);
                unpackdir = directory + directoryName;
                if (directoryName != string.Empty)
                {
                    Directory.CreateDirectory(unpackdir);
                }
                if (fileName != string.Empty)
                {
                    FileStream streamWriter = File.Create(Path.Combine(directory, theEntry.Name));
                    int size = 2048;
                    byte[] data = new byte[size];
                    while (true)
                    {
                        size = zis.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            zis.Close();
            //File.Delete(zipFile);
            return unpackdir;
        }

        public static void CreateHtml(string dirpath, string pagename, string html)
        {
            string filepath = dirpath + pagename + ".html";
            MyFile.CreateFile(html, filepath);
        }

        public static void CreateFile(string content, string filepath) 
        {
            using(StreamWriter sw=new StreamWriter(filepath,false,Encoding.UTF8))
            {
                sw.Write(content);
            }
        }

        public static void CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);
            string desfolderdir = desdir + "\\" + folderName;
            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);
            foreach (string file in filenames)// 遍历所有的文件和目录
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {
                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }
                    CopyDirectory(file, desfolderdir);
                }
                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);
                    srcfileName = desfolderdir+"\\"+ srcfileName;
                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }
                    File.Copy(file, srcfileName);
                }
            } 
        }

        public static void DeleteDir(string todir)
        {
            if (Directory.Exists(todir)) 
            {
                Directory.Delete(todir, true);
            }
        }
    }
}
